package com.ulook.lookplanet.controller;

import com.ulook.lookplanet.bean.TTopic;
import com.ulook.lookplanet.bean.VArticlePreInfo;
import com.ulook.lookplanet.common.ExceptionCode;
import com.ulook.lookplanet.common.JsonResult;
import com.ulook.lookplanet.service.SquareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @date 2021/4/15
 */
@RestController
@RequestMapping("/square")
public class SquareController {

    @Autowired
    private SquareService service;

    /**
     * 获取热点话题
     */
    @PostMapping("/getHotTopics")
    public JsonResult<List<TTopic>> getHotTopic(HttpServletRequest request){
        List<TTopic> hotTopics = service.getHotTopic();
        if(hotTopics.size() > 0){
            return new JsonResult<>(hotTopics);
        }
        return new JsonResult<>(ExceptionCode.NO_DATA, "未查询到热点话题");
    }

    /**
     * 获取推荐动态
     */
    @PostMapping("/loadArticles")
    public JsonResult<List<VArticlePreInfo>> getSquareArticle(HttpServletRequest request){
        String currUserID = request.getParameter("currUserId");
        if(currUserID == null || "".equals(currUserID)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        String itemNum = request.getParameter("itemNum");

        List<VArticlePreInfo> articlePreInfos = service.getSquareArticle(Integer.parseInt(currUserID), Integer.parseInt(itemNum));
        if(articlePreInfos.size() == 0){
            return new JsonResult<>(ExceptionCode.NO_DATA, "没有更多动态了");
        }
        return new JsonResult<>(articlePreInfos);
    }
}
